<style type="text/css">
    .demo-upload-list {
        display: inline-block;
        width: 60px;
        height: 60px;
        text-align: center;
        line-height: 60px;
        border: 1px solid transparent;
        border-radius: 4px;
        overflow: hidden;
        background: #fff;
        position: relative;
        box-shadow: 0 1px 1px rgba(0, 0, 0, .2);
        margin-right: 4px;
    }

    .demo-upload-list img {
        width: 100%;
        height: 100%;
    }

    .demo-upload-list-cover {
        display: none;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0, 0, 0, .6);
    }

    .demo-upload-list:hover .demo-upload-list-cover {
        display: block;
    }

    .demo-upload-list-cover i {
        color: #fff;
        font-size: 20px;
        cursor: pointer;
        margin: 0 2px;
    }

    .selected-search {
        box-shadow: 0 1px 6px rgba(0, 0, 0, .2);
        height: 200px;
        width: 260px;
        position: relative;
        border-radius: 4px;
    }

    .selected-search .search-action {
        position: absolute;
        top: 10px;
        left: 10px;
        right: 10px;
    }

    .selected-search .selected-arr {
        position: absolute;
        top: 50px;
        left: 20px;
        right: 20px;
        bottom: 10px;
        overflow: scroll;
    }

    .select-click {
        border: 1px solid #dddee1;
        border-radius: 4px;
        padding: 0 10px;
        margin-bottom: 10px;
    }

    .selected-search .selected-arr .selected-item {
        /*border-bottom: 1px solid #dfdfdf;*/
    }
</style>
<template>
    <div style="min-width:1024px;">
        <h4>用车申请</h4>
        <Alert show-icon>
            填写用车申请，用于申请对管理车辆的使用。填写完成之后需要点击“申请”按钮提交申请进行审批。
        </Alert>
        <Row style="margin-top:15px;">
            <Col span="13">
                <i-form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="110" >
                    <Row>
                        <Col span="12">
                            <selectSearch :selectBaseInfo="selectBaseInfo" @post-data="getCompentensData"></selectSearch>
                        </Col>
                        <Col span="12">
                            <Form-item label="目的地" prop="Destination">
                                <i-input v-model="formValidate.Destination" placeholder="请输入目的地""></i-input>
                            </Form-item>
                        </Col>
                    </Row>

                    <Form-item label="用车原因" prop="Reason">
                        <i-input type="textarea" v-model="formValidate.Reason" placeholder="用车原因" :rows="4"></i-input>
                    </Form-item>
                    <Row>
                        <Col span="12">
                            <Form-item label="选择车队" prop="Carmgrorgid">
                                <i-select v-model="formValidate.Carmgrorgid" placeholder="请选择车队">
                                    <template v-for="(item,index) in carArr">
                                        <i-option :value="item.Id">{{item.Title}}</i-option>
                                    </template>
                                </i-select>
                            </Form-item>
                        </Col>
                        <Col span="12">
                            <Form-item label="选择用车类型" prop="Usedcartype">
                                <i-select v-model="formValidate.Usedcartype" placeholder="请选择用车类型">
                                    <template v-for="(item) in carTypeArr">
                                        <i-option :value="item.Id" v-bind:key="item.Id">{{item.Title}}</i-option>
                                    </template>
                                </i-select>
                            </Form-item>
                        </Col>
                    </Row>
                    <Row>
                        <Col span="12">
                            <Form-item label="用车开始时间">
                                <Date-picker type="datetime" placeholder="选择开始日期" @on-change="changeStartDate"></Date-picker>
                            </Form-item>
                        </Col>
                        <Col span="12">
                            <Form-item label="用车结束时间">
                                <Date-picker type="datetime" placeholder="选择结束日期"  @on-ok="getCheckerData" @on-change="changeEndDate"></Date-picker>
                            </Form-item>
                        </Col>
                    </Row>
                    <Form-item label="下级审核员" prop="reason">
                        <Select  v-model="checkSelection" prefix="md-search" style="width:200px">
                            <Option v-for="item in checkPeopleArr" :value="item.Id" :key="item.Id">{{ item.Account}}
                            </Option>
                        </Select>
                    </Form-item>
                    <Alert>
                        <b>通行门区设置</b>
                        <Row>
                            <Col span="12">
                                <Form-item label="允许通行门区" prop="Accessgates">
                                    <i-select style="width:200px" placeholder="选择允许通行门区" multiple :model.sync="formValidate.Accessgates"
                                            filterable @on-change="checkChangeGate">
                                        <i-option v-for="item in geteListArr" :value="item.Id">{{ item.Gatename }}</i-option>
                                    </i-select>
                                </Form-item>
                            </Col>
                            <Col span="12">
                                <Form-item label="通行类型" prop="Accesstype">
                                    <i-select v-model="formValidate.Accesstype" placeholder="请选择审核状态">
                                        <i-option value="0">单次进出</i-option>
                                        <i-option value="1">重复进出</i-option>
                                    </i-select>
                                </Form-item>
                            </col>
                        </Row>
                    </Alert>
                    <Form-item>
                        <i-button @click="handleSubmit('formValidate')" type="primary">&nbsp;&nbsp;&nbsp;&nbsp;申&nbsp;&nbsp;请&nbsp;&nbsp;&nbsp;&nbsp;</i-button>
                        <i-button @click="jumpPage('','supplierAllBack')"  style="margin-left: 8px">重填</i-button>
                    </Form-item>
                </i-form>
            </Col>
        </Row>

        <Modal v-model="isShowModal2" title="选择部门" @on-ok=""
               @on-cancel="isShowModal2 =false">
            <Tree :show-checkbox="showCheckbox" :data="orgData" @on-select-change="orgClickEvent"></Tree>
        </Modal>
    </div>
</template>
<script>

    import apiUtil from '../../assets/utils/api_util';
    import selectSearch from '../../components/selectSearch';

    /*
     * */


    export default {
        data() {
            return {
                //组织选择树形
                showCheckbox: false,
                isShowModal2: false,
                orgData: [],
                //部门名称、id
                orgId: '',
                orgName: '',
                //单位名称、id
                unitname:'',
                unitid:'',
                //表单数据
                formValidate: {
                    //添加字段
                    Carmgrorgid:'',
                    Reason:'',
                    Usedstartdate:'',
                    Usedenddate:'',
                    Peoplename:'',
                    Usedcartype:'',
                    Accessgates:'',
                    Accesstype:'',
                    Destination:'',
                },
                ruleValidate: {},
                userDetailInfo: '',
                //车队列表
                carArr: [],
                //车类型
                carTypeArr: [],
                //门区列表
                geteListArr: [],
                //选择部门
                selectBaseInfo: {
                    "title": '选择人员',
                    "placeholder": '请输入姓名',
                    "postUrl": apiUtil.urls.userList,
                    'postType': 'user',
                },
                reqpeopleDept: '',
                //请假人选择操作
                principalId: '',
                principalArr: [],
                personalName: '请选择请假人',
                selectPeopelIsShow: false,
                papertypeArr: [],
                //是否禁用
                isDisabled: true,
                //审核人员
                checkPeopleArr:[],
                checkSelection:[],
            }
        },
        mounted: function () {
            this.getCarArr();
            this.getCarTypeArr();
            this.getGateList();
        },

        components: {
            selectSearch
        },
        methods: {
            jumpPage: function (id, url) {
                //跳转
                let route = '';
                if (id.length > 0) {
                    route = {
                        path: url,
                        query: {
                            id: id
                        },
                    };
                } else {
                    route = {
                        path: url,
                    };
                }

                this.$router.push(route);
            },
            //获取门区信息
            getGateList() {
                let that = this;

                let para = {};
                let params = that.$qs.stringify(para);

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.GateServerList, para, function (res) {
                    that.$Loading.finish();
                    that.geteListArr = res.Msg;
                });

            },
            //选择门区
            checkChangeGate: function (data) {
                console.log('多选值:', data);
                let arr = Array.from(data);
                // this.formValidate.Gates =arr.join();
                this.formValidate.Accessgates = arr;
                console.log('checkChangeGate:', this.formValidate.Accessgates);
            },
            handleSubmit(name) {

                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.addData(model);
                })
            },
            //获取车队列表
            getCarArr: function () {
                let that = this;

                let para = {};
                let params = that.$qs.stringify(para);

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.CarMgrDeptList, params, function (res) {
                    that.$Loading.finish();
                    that.carArr = res.Msg;
                });
            },
            //获取车类型
            getCarTypeArr: function () {
                let that = this;

                let para = {};
                let params = that.$qs.stringify(para);

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.CarUseTypesList, params, function (res) {
                    that.$Loading.finish();
                    that.carTypeArr = res.Msg;
                });
            },
            changeStartDate: function (date) {
                console.log(date);
                this.formValidate.Usedstartdate = date;
            },
            changeEndDate: function (date) {
                console.log(date);
                this.formValidate.Usedenddate = date;
            },
            changeStartDate2: function (date) {
                console.log(date);
                this.formValidate.starttime = date;
                console.log('starttime:',this.formValidate.starttime);

            },
            changeEndDate2: function (date) {
                console.log(date);
                this.formValidate.endtime = date;
            },
            //获取选择搜索组件传来结果数据
            getCompentensData: function (value) {
                this.reqpeopleDept = value;
                // this.getDetail(value.resultId);
                this.isDisabled = true;
            },
            //详情
            getDetail: function (id) {
                let that = this;
                that.$Loading.start();

                let para = {
                    id: id,
                };

                apiUtil.get(that, apiUtil.urls.userClientDetail, para, function (res) {
                    that.userDetailInfo = res.Msg;
                    that.orgName = res.Msg.OrgName;
                    that.orgId = res.Msg.Orgid;
                    that.unitid = res.Msg.Mainorgid;
                    that.unitname = res.Msg.MainOrgName;
                    that.formValidate.Peoplename = res.Msg.Personalname;
                    that.formValidate.Peoplephone = res.Msg.Phone;
                });
            },
            choosePeople: function (id, name) {
                this.principalId = id;
                this.personalName = name;
                this.selectPeopelIsShow = false;
            },
            //获取审核员列表
            getCheckerData() {
                let that = this;

                let para = {
                    accountId:that.reqpeopleDept.resultId,
                    startDate:that.formValidate.Usedstartdate,
                    endDate:that.formValidate.Usedenddate,
                };
                let params = that.$qs.stringify(para);

                this.$Loading.start();

                apiUtil.post(that, apiUtil.urls.GetCheckerListCar, params, function (res) {
                    that.$Loading.finish();

                    that.checkPeopleArr = res.Msg;
                });
            },
            //添加
            addData: function (model) {
                let that = this;

                if(!model.Accessgates){
                    that.$Notice.open({
                        title: '通知',
                        desc: '请选择门区 ',
                        duration: 0
                    });
                    return
                }
                let selectedItemIds = "";

                for (var i = 0; i < model.Accessgates.length; i++) {
                    selectedItemIds += model.Accessgates[i].toString() + ",";
                }
                selectedItemIds = selectedItemIds.substring(0, selectedItemIds.length - 1);

                let Ruleresult =[{"Index": 1,"CheckAccountId":that.checkSelection}];

                let para ={
                    Accountid:that.reqpeopleDept.resultId,
                    Reason:model.Reason,
                    Carusetypeid:model.Usedcartype,
                    Destination:model.Destination,
                    Accessgates:selectedItemIds,
                    Accesstype:model.Accesstype,
                    Startdate:model.Usedstartdate,
                    Enddate:model.Usedenddate,
                    Ruleresult: JSON.stringify(Ruleresult),
                    Creatorid:sessionStorage.getItem('userId'),
                }

                let params = that.$qs.stringify(para);
                this.$Loading.start();
                console.log('发送数据:', model);

                apiUtil.post(that, apiUtil.urls.AddCarUseRequest, params, function (res) {
                    that.$Loading.finish();
                    that.$Message.success('申请成功 ');
                });
            },

            uploadList() {
                let that = this;
                this.imageList = that.$refs.upload ? that.$refs.upload.fileList : [];
                console.log('this.imageList:', this.imageList);
            },

        }
    }
</script>
